package br.com.kliptus.web.jsf.managedbean;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.event.ActionEvent;

import org.maverick.web.jsf.managedbean.ManagedBeanBase;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import br.com.kliptus.application.ApplicationConstants;
import br.com.kliptus.model.bean.UserJB;
import br.com.kliptus.persistence.dao.UserDAO;

@ManagedBean(name = "authenticationMB")
@ViewScoped
public class AuthenticationMB extends ManagedBeanBase {

	private static final long serialVersionUID = 1L;
	
	private String userName;
	private String password;
	
	@Autowired
	@Qualifier("userDAOImpl")
	private UserDAO dao;

	public AuthenticationMB() {
		super();
		initComponents();
	}
	
	@Override
	public void initComponents() {
		
	}
	
	public String login() {
		
		UserJB user = this.dao.getUser(userName, password);
		
		if (user != null) {
			super.setSessionAttribute(ApplicationConstants.USER_SESSION, user);
			return ApplicationConstants.PAGE_INDEX;
		}
		
		return null;
	}

	@Override
	public void save(ActionEvent event) {
		
	}

	@Override
	public void remove(ActionEvent event) {
		
	}

	@Override
	public void list() {
		
	}

	@Override
	public Long count() {
		return null;
	}

	@Override
	public void find(Integer firstResult, Integer maxResult) {
		
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

}